<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/9
 * Time: 15:45
 */
namespace Admin\Controller;
use Admin\Model\BaseinfoVersionModel;
use Admin\Model\InspectiondetailinfoModel;
use Base\Controller\CurdController;
class InspectioninfoController extends CurdController
{

    public function __construct()
    {
        parent::__construct(true, true);
    }
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $map['name'] = array('like','%'.$_REQUEST['keyword'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        if(!I('get.del')){
            $where['status']=1;
        }else{
            $where['status']=0;
        }
        if(I('get.typeid')){
            $where['typeid']=I('get.typeid');
        }
         if(I('get.hospitalid')){
            $where['hospitalid']=I('get.hospitalid');
        }

        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
        $data = $this->model->pageToView($limit, true,$where);
//        dump($data['list']);
        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);

    }
    /**
     * 编辑界面
     * @param $id
     */
    public function showEdit($id)
    {
        if (!$id) {
            $this->error('非法操作');
        }
        $select = $this->model->find($id);
        if (empty($select)) {
            $this->error('非法操作');
        }
       $InspectiondetailinfoModel = new  InspectiondetailinfoModel();
        $itemid=$InspectiondetailinfoModel->where(array('inspectionid'=>$id,'status'=>1))->select();
        foreach ($itemid as $k=>$v){
            $mediclaid[] =$v['itemid'];
        }
        $this->assign('data', $select);
        $this->assign('itemid', $mediclaid);
        $this->show('');
}
    /**
     * 编辑
     * */
    public function edit()
    {
        $data['id']=$_REQUEST['id'];
        $data['name']=$_REQUEST['name'];
        $data['typeid']=$_REQUEST['typeid'];
        $data['description']=$_REQUEST['description'];
		$data['sort']=$_REQUEST['sort'];
        $itemid=$_REQUEST['itemid'];
	          //   dump($itemid);
          //    return falsel;
        if (!$data['name']) {
            $this->error('分类名称不能为空');
        } else {
            $InspectiondetailinfoModel = new  InspectiondetailinfoModel();
            $InspectiondetailinfoModel->where(array('inspectionid'=>$data['id']))->save(array('status'=>0));
                foreach ($itemid as $value){
				//dump($value);
                    $result=$InspectiondetailinfoModel->where(array('inspectionid'=>$data['id'],'itemid'=>$value))->find();
                    if($result){
                        $save['id']=$result['id'];
                        $save['status']=1;
                        $InspectiondetailinfoModel->data($save)->save();
                    }else{
                        $InspectiondetailinfoModel=new InspectiondetailinfoModel();
                        $ids=$InspectiondetailinfoModel->order('id desc')->find();
                        $where['id']=$ids['id']+1;
                        $where['inspectionid'] = $data['id'];
                        $where['itemid'] = $value;
                        $where['status'] = 1;
					//print_r($where);
					//return false;
                        $InspectiondetailinfoModel->add($where);
                    }
                }
          
         $this->editInspectiondetailversion($data['id']);
            $data=$this->editAfter($data);
            $datas = $this->model->save($data);
            if ($datas) {
                $this->success('操作成功', $this->urlConfig['edit_success']);
            } else {
                $this->success('操作成功', $this->urlConfig['edit_success']);
            }
        }
    }
    /**
     * @param $id
     */
    public  function detailinfolist($id){
        if (!$id) {
            $this->error('非法操作');
        }
        $InspectiondetailinfoModel= new InspectiondetailinfoModel();
        $list =$InspectiondetailinfoModel->relation(true)->where(array('inspectionid'=>$id,'status'=>1))->select();
// dump($list);
        $this->assign('data', $list);
        $this->show('');
    }
    protected function addAfter($data)
    {
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        $ids=$this->model->order('id desc')->find();
        $data['id']=$ids['id']+1;
		if(!$data['sort']){
			$data['sort']=0;
		}
        return $data;
    }

    protected function editAfter($data)
    {
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        return $data;
    }

    /**
     * 修改药单详情版本号
     */
    public  function  editInspectiondetailversion($inspectionid){
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $InspectiondetailinfoModel= new  InspectiondetailinfoModel();
        $info=$BaseinfoVersionModel->where(array('name'=>'inspectiondetailinfo'))->find();
        $data['id']=$info['id'];
        $data['version']=$info['version']+1;;
        $res= $BaseinfoVersionModel->save($data);
        if($res){
            $infos=$BaseinfoVersionModel->find($info['id']);
            $where['inspectionid']=$inspectionid;
            $dataes['version']=$infos['version'];
            $InspectiondetailinfoModel->where($where)->data($dataes)->save();
        }
    }
}
